#include<iostream>
#include<assert.h>
using namespace std;

namespace yangyi 
{
	template <class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{

		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		iterator capacity()
		{
			return _end_of_storage - _start;
		}
		iterator size()
		{
			return _finish - _start;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t size = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * size);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + size;
				_end_of_storage = _start + n;
			}
		}
		void push_back(const T& x)
		{
			if (size() == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			_start[_finish] = x;
			++_finish;
		}
		void insert(iterator pos, const T& x)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			if (_start == _finish)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = len + _start;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				_start[end + 1] = _start[end];
				--end;
			}
			_start[pos] = x;
			++_finish;
		}
		void erase(iterator pos)
		{
			assert(_start < _finish);
			iterator begin = pos + 1;
			while (end < _finish)
			{
				_start[begin - 1] = _start[begin];
				++_begin;
			}
			--_finish;
		}
		
	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}
//ostream& operator<<(ostream& out, vector<T>& s )
//{
//	for ()
//	{
//
//	}
//}